package contents

import (
	"github.com/pkg/errors"
	"yizhuoyan/go-study/pdfgo/raw/objects/basicobject"
	"yizhuoyan/go-study/pdfgo/raw/objects/indirectobject"
)

type Resources struct {
	dic        *basicobject.Dictionary
	locator    indirectobject.Locator
	ExtGState  *basicobject.Dictionary
	ColorSpace *basicobject.Dictionary
	Pattern    *basicobject.Dictionary
	Shading    *basicobject.Dictionary
	XObject    *basicobject.Dictionary
	Font       *basicobject.Dictionary
	ProcSet    *basicobject.Array
	Properties *basicobject.Dictionary
}

func (this *Resources) LoadXObject() *basicobject.Dictionary {
	return this.dic.MustGetDictionary("/XObject")
}
func (this *Resources) ParseFrom(value basicobject.BasicObject, locator indirectobject.Locator) error {
	if value == nil {
		return nil
	}
	if refRst, is := value.(*basicobject.IndirectObjectReference); is {
		indirectObject, err := locator.LoadIndirectObject(refRst)
		if err != nil {
			return err
		}
		return this.ParseFrom(indirectObject.MustGetDictionary(), locator)
	} else if dicRst, is := value.(*basicobject.Dictionary); is {
		return this.parseFromDictionary(dicRst, locator)
	}
	return errors.Errorf("Resources仅支持dictionary和ior")
}
func (this *Resources) parseFromDictionary(dic *basicobject.Dictionary, locator indirectobject.Locator) error {
	this.dic = dic
	this.XObject = dic.MustGetDictionary("/XObject")
	this.locator = locator
	return nil
}
